//
//  RW_CopyrightView.m
//  SleepApp
//
//  Created by chuangqi on 2021/4/13.
//  Copyright © 2021 RiceWang. All rights reserved.
//

#import "RW_CopyrightShowView.h"
#import "RW_UserSongModel.h"

@interface RW_CopyrightShowView ()<UITextViewDelegate>{
    RW_UserSongModel *_user_song_model;
    RW_SongModel *_song_model;
}
/** 关闭 */
@property (nonatomic,strong) UIButton *wlclass_close_button;
/** 内容 */
@property (nonatomic,strong) UITextView *desc_label;
/** 标题 */
@property (nonatomic,strong) UILabel *title_label;

@end

@implementation RW_CopyrightShowView
#pragma mark -- 视图初始化 必须实现
/** 初始化视图数据 */
-(void)initView{
    self.backgroundColor = UIColor.whiteColor;

    self.clipsToBounds = YES;
    self.layer.cornerRadius = 11;
    self.backgroundColor = [UIColor colorWithHexString:@"ffffff"];
    [self.contentView addSubview:self.title_label];
    [self.contentView addSubview:self.desc_label];
    [self.contentView addSubview:self.wlclass_close_button];
    
    [self.title_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).mas_offset(20);
        make.right.mas_equalTo(self.contentView).mas_offset(-20);
        make.top.mas_equalTo(self.contentView).mas_offset(10);
    }];
    [self.desc_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).mas_offset(20);
        make.right.mas_equalTo(self.contentView).mas_offset(-20);
        make.top.mas_equalTo(self.title_label.mas_bottom).mas_offset(10);
    }];
    [self.wlclass_close_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(self.contentView);
        make.height.mas_offset(40);
    }];
}
/** 初始化视图数据 */
-(void)initData:(NSArray *)dataList{
    for (id obj in dataList) {
        if ([obj isKindOfClass:RW_UserSongModel.class]){
            _user_song_model = obj;
            break;
        } else if ([obj isKindOfClass:RW_SongModel.class]){
            _song_model = obj;
            break;
        }
    }
    self.title_label.text = @"内容所属";
    [self uploadDesc];
}
#pragma mark -- Base代理
/**
 可选
 视图弹出方向
 */
-(Base_General_Show_type)showViewLocation{
    return Base_General_Show_type_Botton;
}
/**
 可选
 判断是否展示弹窗
 需要展示 则调用isShowBlock()回调
 默认展示
 */
-(void)isShowModelList:(NSArray<id> *)dataList isShowBlock:(void(^)(void))isShowBlock{
    NSString *string = @"";
    for (id obj in dataList) {
        if ([obj isKindOfClass:RW_UserSongModel.class]){
            RW_UserSongModel *m = obj;
            string = m.fromAuthor;
            break;
        } else if ([obj isKindOfClass:RW_SongModel.class]){
            RW_UserSongModel *m = obj;
            string = m.fromAuthor;
            break;
        }
    }

    if (string.length){
        isShowBlock();
    } else {
        [SVProgressHUD showError_wl:@"无版权信息!"];
    }
}
/**
 可选
 初始化展示视图约束
 默认的大小为屏幕高度一半，底部贴合
 */
-(void)initViewMakeConstraints:(MASConstraintMaker *)make fromView:(UIView *)fromView{
    make.center.mas_equalTo(fromView);
    make.width.mas_equalTo(fromView).multipliedBy(.8f);
    make.height.mas_offset(210);
}
#pragma mark -- 操作
-(void)uploadDesc{
    NSString *authorName = @"";
    if (_user_song_model){
        authorName = _user_song_model.fromAuthor;
    } else if (_song_model){
        authorName = _song_model.fromAuthor;
    }
    NSArray *array = [authorName componentsSeparatedByString:@" "];

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:[array componentsJoinedByString:@"\n"]];
    
    [attributedString addAttribute:NSLinkAttributeName
                             value:@"help://"
                             range:[[attributedString string] rangeOfString:[array lastObject]]];
    // 设置段落
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 3;
    paragraphStyle.alignment = NSTextAlignmentCenter;
    //设置样式
    [attributedString addAttributes:@{
        NSParagraphStyleAttributeName:paragraphStyle,
        NSKernAttributeName:@.5f,
        NSFontAttributeName:[UIFont systemFontOfSize:14]
    } range:NSMakeRange(0, attributedString.length)];
    
    _desc_label.attributedText = attributedString;
}
#pragma mark -- 懒加载
-(UILabel *)title_label{
    if (!_title_label){
        _title_label = [[UILabel alloc]init];
        _title_label.textAlignment = NSTextAlignmentCenter;
        _title_label.text = @"内容所属";
        _title_label.textColor = HexColor(@"333333");
        _title_label.font = [UIFont boldSystemFontOfSize:19];
    }
    return _title_label;
}
-(UITextView *)desc_label{
    if (!_desc_label){
        _desc_label = [[UITextView alloc]init];
        _desc_label.textColor = HexColor(@"666666");
        _desc_label.linkTextAttributes = @{NSForegroundColorAttributeName:HexColor(@"FF3D38")};
        _desc_label.delegate = self;
        _desc_label.editable = NO;        //必须禁止输入，否则点击将弹出输入键盘
        _desc_label.scrollEnabled = NO;
    }
    return _desc_label;
}
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    if ([[URL scheme] isEqualToString:@"help"]) {
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string = textView.text;
        [SVProgressHUD showSuccess_wl:@"内容已复制到剪贴板"];
        [self defaultCloseView];
        return NO;
    }
    return YES;
}
-(UIButton *)wlclass_close_button{
    if (!_wlclass_close_button){
        _wlclass_close_button = [[UIButton alloc]init];
        [_wlclass_close_button setTitle:@"知道了" forState:UIControlStateNormal];
        [_wlclass_close_button setTitleColor:HexColor(@"FF3D38") forState:UIControlStateNormal];
        _wlclass_close_button.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        [_wlclass_close_button addTarget:self action:@selector(defaultCloseView) forControlEvents:UIControlEventTouchUpInside];

        UIView *v = [[UIView alloc]init];
        v.backgroundColor = HexColor(@"E1E1E1");
        [_wlclass_close_button addSubview:v];
        [v mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.mas_equalTo(_wlclass_close_button);
            make.height.mas_offset(1);
        }];
    }
    return _wlclass_close_button;
}
@end
